package com.Houserentsys.service;

import com.Houserentsys.domain.House;

public class HouseService {

    //创建house数组对象
    private House[]  houses; //公用对象
    int houseNum = 1;  //实际房屋数量
    int houseIndex = 1;

    public House findId(int id) {//返回house类型
        for (int i = 0; i < houseNum; i++) {
            if (houses[i].getId() == id) {
                return houses[i];
            }
        }
        return null;
    }


    public boolean HouseDel(int delID) {
        int index = -1;

        for (int i = 0; i < houseNum; i++) {
            if (houses[i].getId() == delID) {
                index = i;
            }
        }

        if (index == -1) {
            return false;
        }

        for (int i = index; i < houseNum-1; i++) {
            houses[i] = houses[i+1];
        }
        houses[--houseNum] = null;
        return true;
   }



    //设置分配大小
    public HouseService(int size) {

        houses = new House[size];
        houses[0] = new House(1,"Bruce","999","Street","1000","出租");
    }



    public boolean add(House house) {

        if(houseNum == houses.length){
            System.out.println("已经满了");
            return false;
        }

        //新增加一个房屋
        houses[houseNum++] = house;

        //设置计数器，自增长房源数量
        house.setId(houseIndex++);
        return true;
    }

    //打印房屋列表信息
    public House[] list(){
        return houses;
    }

}
